using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using EntLibEC.Cmn;
using EntLibEC.Biz.Report;
using EntLibEC.Biz;

namespace EntLibEC.View.Report
{
	/// <summary>
	/// Summary description for PointBalance.
	/// </summary>
	public partial class PointBalance : PageBase
	{
		private int incomeAmountSum, incomeCountSum;
		private int outcomeAmountSum, outcomeCountSum;
		private int transferAmountSum, transferCountSum;

		private int addPointLaterAmountSum, remarkAmountSum, addPointToSysAccountsAmountSum;
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			base.CheckAccess((int)AppEnum.Privilege.FinanceReportSearch,true ,null);
			if ( !Page.IsPostBack )
			{
				txtDateFrom.Text = DateTime.Now.ToString(AppConst.DateFormat).Substring(0,7) + "-01";
				txtDateTo.Text = DateTime.Now.ToString(AppConst.DateFormat);
				btnExport.Enabled = false;
			}
			txtDateFrom.Attributes["onclick"]= "javascript:calendar()";
			txtDateTo.Attributes["onclick"]= "javascript:calendar()";
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);

		}
		#endregion

		private bool checkForm()
		{
			if ( !checkDate(txtDateFrom, "Date From") || !checkDate(txtDateTo, "Date To"))
				return false;

			return true;

		}
		private bool checkDate(TextBox txt, string txtName)
		{
			if ( txt.Text.Trim() == "")
			{
				Assert(lblMessage, txtName + " can't be empty", -1);
				return false;
			}
			else
			{
				try
				{
					Convert.ToDateTime(txt.Text.Trim());
				}
				catch
				{
					Assert(lblMessage, txtName + " format error", -1);
					return false;
				}
			}
			return true;

		}

		protected void btnSearch_Click(object sender, System.EventArgs e)
		{
			if ( !checkForm())
				return;
			Hashtable ht = new Hashtable(2);
			ht.Add("DateFrom", txtDateFrom.Text.Trim());
			ht.Add("DateTo", txtDateTo.Text.Trim());

			DataSet ds = ReportManager.GetInstance().GetPointBalanceDs(ht);

			DataGrid1.DataSource = ConvertDs(ds);
			DataGrid1.DataBind();

			int rows = 0;
			if ( Util.HasMoreRow(ds) )
			{
				rows = ds.Tables[0].Rows.Count;
				btnExport.Enabled = true;
			}
			Assert(lblMessage, rows.ToString() + " record(s)", 1);
		}

		private DataSet ConvertDs(DataSet ds)
		{
			if ( !Util.HasMoreRow(ds))
				return null;

			ds.Tables[0].Columns.Add("IncomeAmountCvt");
			ds.Tables[0].Columns.Add("OutcomeAmountCvt");
			ds.Tables[0].Columns.Add("TransferAmountCvt");

			ds.Tables[0].Columns.Add("AddPointLaterAmountCvt");
			ds.Tables[0].Columns.Add("RemarkAmountCvt");
			ds.Tables[0].Columns.Add("AddPointToSysAccountsAmountCvt");

			foreach( DataRow dr in ds.Tables[0].Rows )
			{
				int incomeAmount, outcomeAmount, transferAmount, incomeCount, outcomeCount, transferCount;
				int addPointLaterAmount,remarkAmount,addPointToSysAccountsAmount;

				incomeAmount = Util.TrimIntNull(dr["incomeAmount"]);
				outcomeAmount = Util.TrimIntNull(dr["outcomeAmount"]);
				transferAmount = Util.TrimIntNull(dr["transferAmount"]);
				incomeCount = Util.TrimIntNull(dr["incomeCount"]);
				outcomeCount = Util.TrimIntNull(dr["outcomeCount"]);
				transferCount = Util.TrimIntNull(dr["transferCount"]);

				addPointLaterAmount = Util.TrimIntNull(dr["AddPointLaterAmount"]);
				remarkAmount = Util.TrimIntNull(dr["RemarkAmount"]);
				addPointToSysAccountsAmount = Util.TrimIntNull(dr["AddPointToSysAccountsAmount"]);



				dr["incomeAmountCvt"] = incomeAmount.ToString(AppConst.IntFormatWithGroup);
				dr["outcomeAmountCvt"] = outcomeAmount.ToString(AppConst.IntFormatWithGroup);
				dr["transferAmountCvt"] = transferAmount.ToString(AppConst.IntFormatWithGroup);


				dr["AddPointLaterAmountCvt"] = addPointLaterAmount.ToString(AppConst.IntFormatWithGroup);
				dr["RemarkAmountCvt"] = remarkAmount.ToString(AppConst.IntFormatWithGroup);
				dr["AddPointToSysAccountsAmountCvt"] = addPointToSysAccountsAmount.ToString(AppConst.IntFormatWithGroup);


				incomeAmountSum += incomeAmount;
				incomeCountSum += incomeCount;
				outcomeAmountSum += outcomeAmount;
				outcomeCountSum += outcomeCount;
				transferAmountSum += transferAmount;
				transferCountSum += transferCount;

				addPointLaterAmountSum += addPointLaterAmount;
				remarkAmountSum += remarkAmount;
				addPointToSysAccountsAmountSum += addPointToSysAccountsAmount;
			}
			return ds;
		}

		private string ToFooterString(int x)
		{
			if ( x == 0)
				return "";
			return x.ToString(AppConst.IntFormatWithGroup);
		}
		private string ToFooterString(decimal x)
		{
			if ( x == 0)
				return "";
			return x.ToString(AppConst.DecimalFormatWithGroup);
		}

		protected void btnExport_Click(object sender, System.EventArgs e)
		{
			Util.ToExcel(DataGrid1, "", "");
		}

		private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
		{
			if(e.Item.ItemType == ListItemType.Footer)
			{
				e.Item.Cells[1].Text = ToFooterString(incomeAmountSum);
				e.Item.Cells[2].Text = ToFooterString(outcomeAmountSum);
				e.Item.Cells[3].Text = ToFooterString(incomeCountSum);
				e.Item.Cells[4].Text = ToFooterString(outcomeCountSum);
				e.Item.Cells[5].Text = ToFooterString(transferAmountSum);
				e.Item.Cells[6].Text = ToFooterString(transferCountSum);

				e.Item.Cells[7].Text = ToFooterString(addPointLaterAmountSum);
				e.Item.Cells[8].Text = ToFooterString(remarkAmountSum);
				e.Item.Cells[9].Text = ToFooterString(addPointToSysAccountsAmountSum);
			}
		}
	
		


	}
}
